package google.entry.update.year;

import db.dao.BaseDao;
import domain.result.Result14;
import domain.task.Task14;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongwei on 2017/1/23.
 */
public class WholeYear4 extends Thread {
    Map<String, Task14> task14Map = new HashMap<>();
    private BaseDao baseDao = new BaseDao();

    @Override
    public void run() {
        try {
            work();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private void work() throws IOException, URISyntaxException {
        List<Task14> task14List = baseDao.find("from Task14");
        for (Task14 task14 : task14List) {
            task14Map.put(task14.getUrl(), task14);
        }
        Map<Integer, List<String>> taskMap = new HashMap<>();
        List<String> periods = FileUtils.readLines(new File("weeks_all.txt"));
        for (int i = 2004; i <= 2016; i++) {
            List<String> list = new ArrayList<>();
            for (String period : periods) {
                if (period.startsWith(String.valueOf(i))) {
                    list.add(period);
                }
            }
            taskMap.put(i, list);
        }
        List<String> lines = FileUtils.readLines(new File("output_14"));
        for (int i = 2004; i <= 2016; i++) {
            List<String> periodsList = taskMap.get(i);
            List<Task14> task14s = new ArrayList<>();
            List<Result14> result14s = new ArrayList<>();
            for (String str : lines) {
                String[] arr = str.split("\t");
                if (!arr[2].trim().equals("0")) continue;
                if (arr[1].startsWith(String.valueOf(i))) {
                    for (String period : periodsList) {
                        Task14 task14 = findTask(arr[0], period);
                        task14.setDone(1);
                        Result14 result14 = new Result14();
                        result14.setCompanyName(arr[0]);
                        result14.setPeriod(period);
                        result14.setNewsCount(0);
                        result14.setTaskId(task14.getId());
                        result14s.add(result14);
                        task14s.add(task14);
                    }
                }
            }
            if (task14s.size() != result14s.size())
                System.exit(-1);
            baseDao.update(task14s, task14s.size());
            baseDao.create(result14s, result14s.size());
            System.out.println(getName() + " -------> " + i);
        }
    }

    private Task14 findTask(String company, String period) {
        String[] arr = period.split("/");
        String cdMin = arr[0].replaceAll("-", "/");
        String cdMax = arr[1].replaceAll("-", "/");
        String url = "[HOST]" + company + "&safe=strict&tbs=cdr:1,cd_min:" + cdMin + ",cd_max:" + cdMax + "&tbm=nws";
        return task14Map.get(url);
    }
}
